//
//  GDRequestManager.m
//  Classfiaction
//
//  Created by apple on 16/1/15.
//  Copyright (c) 2016年 HuaZhengInfo. All rights reserved.
//

#import "GDRequestManager.h"

@implementation GDRequestManager

#pragma mark  数据请求
#pragma mark -- post请求
+ (void)POST:(NSString *)URL params:(NSDictionary *)params success:(void (^)(id))success failure:(void (^)(AFHTTPRequestOperation *, NSError *))Error{

    // 请求管理者
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    // 请求超出时间
    manager.requestSerializer.timeoutInterval = 30.0;
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString * postStr = URL;
    NSDictionary * dict = [params mutableCopy];
    // post请求
    [manager POST:postStr parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSMutableArray * responseArray = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        
        success(responseArray);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        if (!operation.responseObject) {
            NSLog(@"网络传输错误");
        }
        Error(operation,error);
    }];
}

#pragma mark -- get请求
+ (void)GET:(NSString *)URL success:(void (^)(id))success failure:(void (^)(AFHTTPRequestOperation *, NSError *))Error{

    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [manager.requestSerializer setHTTPShouldHandleCookies:NO];
    manager.requestSerializer.timeoutInterval = 30.0;
    NSString * getStr = URL;
    // get 请求
    [manager GET:getStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary * responseDict = (NSDictionary *)responseObject;
        success(responseDict);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        if (!operation.responseObject) {
            NSLog(@"网络传输错误");
        }
        Error(operation,error);
        
        
    }];
}

#pragma mark -- 上传图片
+ (void)UPLOADIMAGE:(NSString *)URL params:(NSDictionary *)params uploadImage:(UIImage *)image success:(void (^)(id))success failure:(void (^)(AFHTTPRequestOperation *, NSError *))Error{

    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer =[AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.requestSerializer.timeoutInterval = 30.0;
    NSString * postStr = URL;
    NSDictionary * dict = [params mutableCopy];
    
    [manager POST:postStr parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        
        NSData * imageData = UIImageJPEGRepresentation(image, 0.1);
        NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"yyyyMMddHHmmss";
        NSString * str = [formatter stringFromDate:[NSDate date]];
        NSString * fileName = [NSString stringWithFormat:@"%@.png",str];
        
        [formData appendPartWithFileData:imageData name:@"img" fileName:fileName mimeType:@"image/png"];
        
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary * responseDict = (NSDictionary *)responseObject;
        success(responseDict);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        Error(operation ,error);
    }];
}
@end
